<template>
	<el-dialog :title="form.id ? '编辑合同' : '新增合同'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="项目编号" prop="contractNumber">
						<el-select
							:disabled="form.id ? true : false"
							v-model="form.projectNumber"
							filterable
							remote
							reserve-keyword
							placeholder="请输入项目编号或者名称"
							:remote-method="getProjectList"
							:loading="loading"
							style="width: 240px"
							@change="selectChange"
						>
							<el-option v-for="item in projectList" :key="item.projectNumber" :label="item.projectNumber" :value="item.projectNumber" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="项目名称" prop="contractName">
						<el-input v-model="form.projectName" disabled placeholder="请输入项目名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="合同名称" prop="contractName">
						<el-input v-model="form.contractName" placeholder="请输入合同名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="合同编号" prop="contractNumber">
						<el-input disabled v-model="form.contractNumber" placeholder="合同编号系统生成" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="签订日期" prop="signDate">
						<el-date-picker v-model="form.signDate" placeholder="项目立项日期" value-format="YYYY-MM-DD" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="合同类型" prop="contractType">
						<el-select v-model="form.contractType">
							<el-option v-for="item in contract_type" :key="item.id" :label="item.label" :value="item.value" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="合同金额" prop="contractMoney">
						<el-input v-model="form.contractMoney" placeholder="请输入合同金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="流转日期" prop="circulationDate">
						<el-date-picker type="date" placeholder="请选择流转日期" v-model="form.circulationDate" value-format="YYYY-MM-DD"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="合同内容" prop="contractContent">
						<editor v-if="visible" v-model:get-html="form.contractContent" placeholder="请输入合同内容"></editor>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="附件" prop="fileList">
						<UploadV2 v-model="form.fileList" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ProjectContractDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/projectContract';
import { fetchList as fetchProjectList } from '@/api/oa/projectBasicInfo';
import UploadV2 from '@/components/UploadV2/index.vue';

const { contract_type } = useDict('contract_type');

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
// 提交表单数据
const form = ref(generateForm());

function generateForm() {
	return {
		id: '',
		projectName: '',
		contractNumber: '',
		contractName: '',
		contractContent: '',
		contractMoney: '',
		contractType: '',
		contractSort: '',
		signStatus: '',
		signDate: '',
		circulationDate: '',
		projectNumber: '',
		fileList: [],
	};
}

const projectList = ref();
// 定义校验规则
const dataRules = ref({
	contractName: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
	contractType: [{ required: true, message: '合同类型不能为空', trigger: 'blur' }],
	contractSort: [{ required: true, message: '合同序号不能为空', trigger: 'blur' }],
	signDate: [{ required: true, message: '签订日期不能为空', trigger: 'blur' }],
	projectNumber: [{ required: true, message: '项目编号不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.value.id = '';

	// 重置表单数据
	nextTick(() => {
		form.value = generateForm();
		dataFormRef.value?.resetFields();
	});

	// 获取projectContract信息
	if (id) {
		form.value.id = id;
		getProjectContractData(id);
	}
};

const selectChange = async (e: any) => {
	form.value.projectName = projectList.value.find((item: any) => item.projectNumber == e).projectName;
};
const getProjectList = async (p: string) => {
	p = p.toUpperCase();
	let params = {
		size: 200,
		projectName: p,
		projectNumber: p,
		projectApprovalStatus: 'approval',
	};
	fetchProjectList(params)
		.then((res: any) => {
			projectList.value = res.data.records;
		})
		.finally(() => {});
};
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.value.id ? await putObj(form.value) : await addObj(form.value);
		useMessage().success(form.value.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getProjectContractData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form.value, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
